package com.briup.cms.service.impl;

import com.briup.cms.bean.BaseConfig;
import com.briup.cms.constant.CmsConstants;
import com.briup.cms.dao.BaseConfigMapper;
import com.briup.cms.dao.extend.BaseConfigExtendMapper;
import com.briup.cms.exception.CustomerException;
import com.briup.cms.service.IBaseConfigService;
import com.briup.cms.utils.MessageStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 基础配置服务层
 */
@Service
public class BaseConfigServiceImpl implements IBaseConfigService{
	
	@Autowired
	private BaseConfigMapper baseConfigMapper;
	@Autowired
	private BaseConfigExtendMapper baseConfigExtendMapper;


	/**
	 * 保存配置信息
	 */
	@Transactional
	@Override
	public void saveConfig(BaseConfig baseConfig) throws CustomerException {
		if(baseConfig==null)
			throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_CAROUSEL_NOT_EXIST);
		baseConfig.setConfigStatus(CmsConstants.SYSTEM_CONFIG_STATUS_NO);
		baseConfigMapper.insert(baseConfig);
	}

	/**
	 * 更新配置信息
	 */
	@Transactional
	@Override
	public void updateConfig(BaseConfig baseConfig) throws CustomerException {
		if(baseConfig==null||baseConfig.getConfigId()==null)
			throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_CONFIG_ID_NOT_NULL);
		if(baseConfigMapper.selectByPrimaryKey(baseConfig.getConfigId())==null)
			throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_CONFIG_NOT_EXIST);
		baseConfigExtendMapper.updateByPrimaryKeySelectiveWithOutStatus(baseConfig);
	}

	/**
	 * 改变配置信息的状态为启用
	 */
	@Transactional
	@Override
	public void changeConfigStatus(int id, int status) throws CustomerException {
		BaseConfig config=baseConfigMapper.selectByPrimaryKey(id);
		if(config==null)
			throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_ARTICLE_NOT_EXIST);
		baseConfigExtendMapper.changeCurrentConfigStatusToNo();
		config.setConfigStatus(CmsConstants.SYSTEM_CONFIG_STATUS_YES);
		baseConfigMapper.updateByPrimaryKeySelective(config);
	}

	/**
	 * 根据id删除
	 */
	@Transactional
	@Override
	public void deleteById(int id)throws CustomerException {
		BaseConfig config=baseConfigMapper.selectByPrimaryKey(id);
		if(config==null)
			throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_ARTICLE_NOT_EXIST);
		baseConfigMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 分页询所有配置信息
	 */
	@Override
	public PageInfo<BaseConfig> findAllWithPage(int pageNum, int pageSize)throws CustomerException {
		if(pageNum<1||pageSize<1)
			throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_ARTICLE_NOT_EXIST);
		PageHelper.startPage(pageNum,pageSize,true);
		List<BaseConfig> list=baseConfigExtendMapper.pageQueryAll();
		return new PageInfo<>(list);
	}

	/**
	 * 查找当前启用的配置信息
	 */
	@Override
	public BaseConfig currentConfigYes() throws CustomerException {
		List<BaseConfig> baseConfigList=baseConfigExtendMapper.findConfigByStstus(CmsConstants.SYSTEM_CONFIG_STATUS_YES);
		if(baseConfigList!=null&&baseConfigList.size()>0)
			return baseConfigList.get(0);
		return new BaseConfig();
	}

}
